package com.zhihuichongcheng.application.ui.cricle.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.cricle.CricleBean;
import com.zhihuichongcheng.application.bean.cricle.HealthBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.cricle.adapter.CricleHealthyAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 添加下啦刷新
 * Created by 3water on 2016/10/24.
 */

public class MyCricleActivity extends BaseActivity{
    private XRecyclerView mRcvCricle;
    private List<CricleBean> mListData = new ArrayList<>();
    private CricleHealthyAdapter mAdapter = null;
    private int mPageSize = 10;
    private int mPage =1;
    private LinearLayout mLlNoData;
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle);
        initView();
        initData();

    }

    private void initView()
    {
        mLlNoData = (LinearLayout) findViewById(R.id.ll_nodata);
        mRcvCricle = (XRecyclerView)findViewById(R.id.rcv_recycle);
        setTitle("我的家园");
        setImRight(R.drawable.ic_add_cricle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(AddCricleActivity.class,0,null);
            }
        });
    }

    private void initData()
    {
        loadData(mPage);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRcvCricle.setLayoutManager(layoutManager);
        mAdapter = new CricleHealthyAdapter(mContext);
        mAdapter.setUserId(mUser.getUserId());
        mRcvCricle.setAdapter(mAdapter);
        for (int i=0;i<20;i++)
        {
            mListData.add(new CricleBean());
        }
        mRcvCricle.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
               mPage =1;
                loadData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });
    }

    private void loadData(final int page)
    {
        HashMap<String,String> params = new HashMap<>();
        params.put("token",mUser.getToken());
        params.put("pageRows",""+mPageSize);
        params.put("curPage",""+page);
        mManager.post(NetworkAPI.MyCricle, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<HealthBean> list = GsonUtil.paraeFromStringToList(value,HealthBean.class);
                if(page ==1)
                {
                    mAdapter.clear();
                    if (list.size() == 0) {
                        mLlNoData.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                        params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                        mLlNoData.setLayoutParams(params);
                        mRcvCricle.setVisibility(View.GONE);
                    } else {
                        mLlNoData.setVisibility(View.GONE);
                        mRcvCricle.setVisibility(View.VISIBLE);
                    }
                }
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvCricle.loadMoreComplete();
                mRcvCricle.refreshComplete();
                if(list.size()<mPageSize)
                {
                    mRcvCricle.setNoMore(true);
                }
            }

            @Override
            public void onFailure(String value) {
                mRcvCricle.loadMoreComplete();
                mRcvCricle.refreshComplete();
                mRcvCricle.setNoMore(true);
                if(page == 1)
                {
                    mLlNoData.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                    params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                    mLlNoData.setLayoutParams(params);
                    mRcvCricle.setVisibility(View.GONE);
                }
            }
        },params);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK)
        {
            mPage =1;
            loadData(mPage);
        }
    }

}
